home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / QuickTime / JPEG Sample / Source / events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-05  |  12.2 KB  |  599 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #        events.c
  4. #
  5. #        This segment handles the basic event calls.
  6. #
  7. #        Author(s):     Michael Marinkovich & Guillermo Ortiz
  8. #                    marink@apple.com
  9. #
  10. #        Modification History: 
  11. #
  12. #            4/3/96        MWM     Initial coding                     
  13. #
  14. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  15. #
  16. #
  17. #        You may incorporate this sample code into your applications without
  18. #        restriction, though the sample code has been provided "AS IS" and the
  19. #        responsibility for its operation is 100% yours.  However, what you are
  20. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  21. #        after having made changes. If you're going to re-distribute the source,
  22. #        we require that you make it clear in the source that the code was
  23. #        descended from Apple Sample Code, but that you've made changes.
  24. #
  25. *************************************************************************************/
  26.  
  27. #include <Devices.h>
  28. #include <DiskInit.h>
  29. #include <Events.h>
  30. #include <ToolUtils.h>
  31. #include <Gestalt.h>
  32. #include <OSUtils.h>
  33. #include <Palettes.h>
  34.  
  35. #include "App.h"
  36. #include "Proto.h"
  37.  
  38. extern Boolean        gInBackground;
  39. extern Boolean        gDone;
  40. extern Boolean        gHasAbout;        // have an about box?
  41.  
  42.  
  43. //----------------------------------------------------------------------
  44. //
  45. //    EventLoop - main entry and loop for all event processing
  46. //
  47. //
  48. //----------------------------------------------------------------------
  49.  
  50. void EventLoop(void)
  51. {
  52.     EventRecord        event;
  53.     RgnHandle        cursorRgn;
  54.     Boolean            gotEvent;
  55.         
  56.     gDone = false;
  57.     cursorRgn = NewRgn();
  58.         
  59.     do 
  60.     {
  61.         gotEvent = WaitNextEvent(everyEvent,&event,MyGetSleep(),cursorRgn);
  62.                                     
  63.         if (gotEvent)
  64.             DoEvent(&event);
  65.  
  66.                         
  67.     } while ( !gDone );
  68.         
  69.     DisposeRgn(cursorRgn);
  70. }
  71.  
  72.  
  73. //----------------------------------------------------------------------
  74. //
  75. //    MyGetSleep - return sleep value based upon whether or not the app
  76. //                 is in the background.
  77. //
  78. //----------------------------------------------------------------------
  79.  
  80. short MyGetSleep(void)
  81. {
  82.     short        sleep = 30;
  83.     
  84.     if (gInBackground)
  85.         sleep = 1L;
  86.  
  87.     return sleep;
  88.  
  89. }
  90.  
  91.  
  92. //----------------------------------------------------------------------
  93. //
  94. //    CustomWindowEvent - Handles custom procs assigned to a window. 
  95. //                        Different window kinds can easily have unique event
  96. //                        handlers, ie. floaters, dialogs, documentprocs
  97. //----------------------------------------------------------------------
  98.  
  99. void CustomWindowEvent(short eventType,WindowRef window,void *refCon)
  100. {
  101.     CustomProc        theProc;
  102.     DocHnd            doc;
  103.     short            kind;
  104.     
  105.     if (nil == window)
  106.         return;
  107.         
  108.     kind = GetWindKind(window);
  109.     if (kind < kDocKind || kind > kAboutKind)     // not our window
  110.         return;
  111.  
  112.     doc = (DocHnd)GetWRefCon(window);
  113.     
  114.     if (doc != nil) 
  115.     {
  116.         HLockHi((Handle)doc);
  117.         switch(eventType) 
  118.         {
  119.             case kIdleProc:
  120.                 theProc = (**doc).idleProc;
  121.                 break;
  122.                 
  123.             case kMenuProc:
  124.                 theProc = (**doc).mMenuProc;
  125.                 break;
  126.  
  127.             case kInContentProc:
  128.                 theProc = (**doc).inContentProc;
  129.                 break;
  130.  
  131.             case kInGoAwayProc:
  132.                 theProc = (**doc).inGoAwayProc;
  133.                 break;
  134.                 
  135.             case kInZoomProc:
  136.                 theProc = (**doc).inZoomProc;
  137.                 break;
  138.                 
  139.             case kInGrowProc:
  140.                 theProc = (**doc).inGrowProc;
  141.                 break;
  142.                 
  143.             case kMUpProc:
  144.                 theProc = (**doc).mUpProc;
  145.                 break;
  146.                 
  147.             case kKeyProc:
  148.                 theProc = (**doc).keyProc;
  149.                 break;
  150.             
  151.             case kActivateProc:
  152.                 theProc = (**doc).activateProc;
  153.                 break;
  154.  
  155.             case kUpdateProc:
  156.                 theProc = (**doc).updateProc;
  157.                 break;
  158.  
  159.             default:
  160.                 theProc = nil;
  161.                 break;
  162.         }        
  163.     
  164.         if (theProc != nil) 
  165.         {
  166.             (*theProc)(window,refCon);
  167.             HUnlock((Handle)doc);
  168.     
  169.         }
  170.     }    
  171.     
  172.     
  173. }
  174.  
  175.  
  176. //----------------------------------------------------------------------
  177. //
  178. //    DoEvent - event dispatcher, called by eventloop
  179. //                
  180. //
  181. //----------------------------------------------------------------------
  182.  
  183. void DoEvent(EventRecord *event)
  184. {
  185.     OSErr            err;
  186.     short            kind;
  187.     long            menuChoice;
  188.     Point            thePoint;
  189.     Boolean            active;
  190.     WindowRef        window;
  191.     
  192.     window = FrontWindow();
  193.  
  194.     switch(event->what) 
  195.     {
  196.         case nullEvent:
  197.             CustomWindowEvent(kIdleProc, window, nil);
  198.             break;
  199.             
  200.         case mouseDown:
  201.             HandleMouseDown(event);
  202.             break;
  203.                             
  204.         case mouseUp:
  205.             break;
  206.                             
  207.         case keyDown:
  208.         case autoKey:
  209.             if (event->modifiers & cmdKey)     //    is cmd key down
  210.             { 
  211.                 menuChoice = MenuKey(event->message & charCodeMask);
  212.                 kind = GetWindKind(window);
  213.                 if (kind < kDocKind || kind > kFloatKind)             // not our window
  214.                     HandleMenuChoice(window, (void *)&menuChoice);    // default menu
  215.                 else    
  216.                     CustomWindowEvent(kMenuProc, window, (void *)&menuChoice);
  217.             }
  218.             break;
  219.                             
  220.         case activateEvt:
  221.             gInBackground = event->modifiers & activeFlag;
  222.             active = gInBackground;
  223.             CustomWindowEvent(kActivateProc, (WindowRef)event->message, &active);
  224.             break;
  225.                             
  226.         case updateEvt:
  227.             UpdateWindow((WindowRef)event->message);
  228.             break;
  229.                             
  230.         case diskEvt:
  231.             if (HiWord(event->message) != noErr) 
  232.             {
  233.                 SetPt(&thePoint, 50, 50);
  234.                 err = DIBadMount(thePoint, event->message);
  235.             }
  236.             break;
  237.                             
  238.         case osEvt:
  239.             switch ((event->message >> 24) & 0x0FF) 
  240.             {        
  241.                 case suspendResumeMessage:    
  242.                     gInBackground = event->message & resumeFlag;
  243.                     active = gInBackground;
  244.                     CustomWindowEvent(kActivateProc, FrontWindow(), &active);
  245.                     break;
  246.             }
  247.             break;
  248.     
  249.         case kHighLevelEvent:
  250.             AEProcessAppleEvent(event);
  251.             break;
  252.     }
  253.  
  254. }            
  255.  
  256.  
  257.  
  258. //----------------------------------------------------------------------
  259. //
  260. //    DoIdle - handle Idle events
  261. //                
  262. //
  263. //----------------------------------------------------------------------
  264.  
  265. void DoIdle(WindowRef window, void *refCon)
  266. {
  267.     #pragma unused (window, refCon)
  268. }
  269.  
  270.  
  271. //----------------------------------------------------------------------
  272. //
  273. //    HandleMouseDown - 
  274. //                
  275. //
  276. //----------------------------------------------------------------------
  277.  
  278. void HandleMouseDown(EventRecord *event)
  279. {
  280.     long            menuChoice;
  281.     short            thePart;
  282.     short            kind;
  283.     WindowRef        window;
  284.         
  285.  
  286.     thePart = FindWindow(event->where,&window);
  287.         
  288.     switch(thePart) 
  289.     {
  290.         case inMenuBar:
  291.             menuChoice = MenuSelect(event->where);
  292.             window = FrontWindow();
  293.             kind = GetWindKind(window);
  294.             if (kind < kDocKind || kind > kAboutKind)             // not our window
  295.                 HandleMenuChoice(window, (void *)&menuChoice);    // default menu
  296.             else    
  297.                 CustomWindowEvent(kMenuProc, window, (void *)&menuChoice);
  298.             break;
  299.  
  300.         case inContent:
  301.             if (window != FrontWindow())
  302.                 SelectWindow(window);
  303.             else
  304.                 CustomWindowEvent(kInContentProc, window, &event->where);
  305.     
  306.             break;
  307.  
  308.         case inSysWindow:
  309.             SystemClick(event,window);
  310.             break;
  311.                                                 
  312.         case inDrag:
  313.             if (window != FrontWindow())
  314.                 SelectWindow(window);
  315.             DragWindow(window, event->where,&qd.screenBits.bounds);
  316.             break;
  317.                         
  318.         case inGoAway:
  319.             if (TrackGoAway(window, event->where))
  320.                 RemoveWindow(window);
  321.             break;
  322.                         
  323.         case inZoomIn:
  324.         case inZoomOut:
  325.             if (TrackBox(window,event->where,thePart)) 
  326.                 CustomWindowEvent(kInZoomProc, window,&thePart);
  327.             break;
  328.                         
  329.         case inGrow:
  330.             CustomWindowEvent(kInGrowProc, window, &event->where);
  331.             break;
  332.     }
  333.     
  334. }
  335.  
  336.  
  337. //----------------------------------------------------------------------
  338. //
  339. //    HandleMenuChoice - 
  340. //                
  341. //
  342. //----------------------------------------------------------------------
  343.  
  344. void HandleMenuChoice(WindowRef window, void *refCon)
  345. {
  346.     OSErr        err = noErr;
  347.     long         menuChoice;
  348.     short        item, menu;
  349.     short        daRefNum;
  350.     Rect        bounds;
  351.     Str255        daName;
  352.     WindowRef    newWindow;
  353.     
  354.     
  355.     menuChoice = *(long *)refCon;
  356.     
  357.     item = LoWord(menuChoice);
  358.     menu = HiWord(menuChoice);
  359.  
  360.     switch(menu) {
  361.         case mApple:
  362.             switch(item) {
  363.                 case iAbout:
  364.                     if ( !gHasAbout ) {
  365.                         SetRect( &bounds,2, 40 ,352 ,140 );
  366.                         newWindow = CreateWindow(nil, nil, &bounds, "\pAbout", true,
  367.                                                  documentProc, kAboutKind, (WindowPtr)-1,
  368.                                                  true, nil );
  369.                         gHasAbout = true;
  370.                     }                        
  371.                     break;
  372.                     
  373.                 default:
  374.                     GetItem(GetMHandle(mApple),item,daName);
  375.                     daRefNum = OpenDeskAcc( daName );
  376.                     break;
  377.             }    
  378.             break;
  379.                     
  380.         case mFile:
  381.             switch(item) {
  382.                 case iNew:
  383.                     SetRect(&bounds,100,100,300,300);
  384.                     newWindow = CreateWindow(128, nil, &bounds, nil, false,
  385.                                             documentProc, kDocKind, (WindowPtr)-1,
  386.                                             true, nil );
  387.                     break;
  388.                 case iOpen:
  389.                     DoOpenNew();
  390.                     break;
  391.                         
  392.                 case iSave:
  393.                     err = SaveJPEG(window);
  394.                     break;
  395.  
  396.                 case iClose:
  397.                     RemoveWindow(FrontWindow());
  398.                     break;    
  399.                         
  400.                 case iQuit:
  401.                     gDone = true;
  402.                     break;
  403.                     
  404.                 default:
  405.                     break;    
  406.             }
  407.             break;
  408.                     
  409.         default:
  410.             break;    
  411.         
  412.     }
  413.     
  414.     HiliteMenu(0);
  415.     
  416. }
  417.  
  418.  
  419. //----------------------------------------------------------------------
  420. //
  421. //    HandleContentClick - 
  422. //                
  423. //
  424. //----------------------------------------------------------------------
  425.  
  426. void HandleContentClick(WindowRef window, void *refCon)
  427. {
  428.     ControlRef            control;
  429.     ControlActionUPP    scrollUPP;
  430.     Point                mouse;
  431.     short                value;
  432.     short                thePart;
  433.     short                oldSetting;
  434.     short                horzScroll,vertScroll;
  435.     DocHnd                doc;
  436.  
  437.     doc = (DocHnd)GetWRefCon(window);
  438.     if (doc != nil) {
  439.         SetPort(window);
  440.         mouse = *(Point *)refCon;
  441.         GlobalToLocal(&mouse);
  442.         horzScroll = vertScroll = 0;
  443.  
  444.         if ((thePart = FindControl(mouse, window, &control)) != 0) {
  445.             switch(thePart) {
  446.                 case inThumb:
  447.                     oldSetting = GetCtlValue(control);
  448.                     thePart = TrackControl(control, mouse, 0L);
  449.                     if (thePart != 0) {
  450.                         value = oldSetting - GetCtlValue(control);
  451.                         if (value != 0){
  452.                             if (control == (**doc).hScroll)
  453.                                 horzScroll = value ;
  454.                             if (control == (**doc).vScroll)
  455.                                 vertScroll = value;
  456.                             MyScrollPicture(window, horzScroll, vertScroll);
  457.                         }
  458.                     }
  459.                     break;
  460.                 case inUpButton:
  461.                 case inDownButton:
  462.                 case inPageUp:
  463.                 case inPageDown:
  464.                     scrollUPP = NewControlActionProc(ScrollActionProc);
  465.                     value = TrackControl(control, mouse, scrollUPP);
  466.                     break;
  467.     
  468.             }
  469.         }
  470.     }
  471.  
  472. }
  473.  
  474.  
  475. //----------------------------------------------------------------------
  476. //
  477. //    HandleZoomClick - 
  478. //                
  479. //
  480. //----------------------------------------------------------------------
  481.  
  482. void HandleZoomClick(WindowRef window, void *refCon)
  483. {
  484.     short            part;
  485.     DocHnd            doc;
  486.     
  487.     doc = (DocHnd)GetWRefCon(window);
  488.     if (doc != nil) {
  489.         part = *(short *)refCon;
  490.         SetPort(window);
  491.         
  492.         EraseRect(&window->portRect);
  493.         ZoomWindow(window,part,true);
  494.         ClipRect(&window->portRect);
  495.         InvalRect(&window->portRect);
  496.         
  497.         HideControl((**doc).hScroll);
  498.         HideControl((**doc).vScroll);
  499.         
  500.         AdjustScrollbars(window, true);
  501.         
  502.         ShowControl((**doc).hScroll);
  503.         ShowControl((**doc).vScroll);
  504.         
  505.     }
  506. }
  507.  
  508.  
  509. //----------------------------------------------------------------------
  510. //
  511. //    HandleGrow - 
  512. //                
  513. //
  514. //----------------------------------------------------------------------
  515.  
  516. void HandleGrow(WindowRef window, void *refCon)
  517. {
  518.     Rect            gIRect;
  519.     Rect            limitRect;
  520.     long            growSize;
  521.         
  522.     limitRect = (**GetGrayRgn()).rgnBBox;
  523.     limitRect.left += 125;
  524.     limitRect.top += 125;
  525.  
  526.     growSize = GrowWindow(window, *(Point *)refCon, &limitRect);
  527.     if (growSize) {
  528.         SetPort(window);    
  529.         gIRect = window->portRect;
  530.         gIRect.top = gIRect.bottom - kScrollWidth;
  531.         gIRect.left = gIRect.right - kScrollWidth;     
  532.         EraseRect(&gIRect);
  533.         SizeWindow(window,LoWord(growSize),HiWord(growSize),true);
  534.         
  535.         ClipRect(&window->portRect);
  536.         AdjustScrollbars(window, true);
  537.         InvalRect(&window->portRect);
  538.     }
  539.  
  540. }
  541.  
  542.  
  543. //----------------------------------------------------------------------
  544. //
  545. //    UpdateWindow - update dispatcher for document windows.
  546. //                 
  547. //
  548. //----------------------------------------------------------------------
  549.  
  550. void UpdateWindow(WindowRef window) 
  551. {
  552.     GrafPtr        oldPort;
  553.     
  554.     
  555.     GetPort(&oldPort);
  556.     
  557.     SetPort(window);
  558.     BeginUpdate(window);
  559.     CustomWindowEvent(kUpdateProc, window, nil);
  560.     EndUpdate(window);
  561.     
  562.     SetPort(oldPort);
  563.  
  564. }
  565.  
  566.  
  567. //----------------------------------------------------------------------
  568. //
  569. //    DoActivate - 
  570. //                 
  571. //
  572. //----------------------------------------------------------------------
  573.  
  574. void DoActivate(WindowRef window, void *refCon)
  575. {
  576.     Boolean        becomingActive;
  577.     DocHnd        doc;
  578.     
  579.     SetPort(window);
  580.         
  581.     doc = (DocHnd)GetWRefCon(window);
  582.  
  583.     if(doc != nil && GetIsAppWindow(window)) {
  584.         becomingActive = *(Boolean *)refCon;
  585.         if (becomingActive) {
  586.             DrawGrowIcon(window);
  587.             ShowControl((**doc).hScroll);
  588.             ShowControl((**doc).vScroll);
  589.         }
  590.         else {
  591.             HideControl((**doc).hScroll);
  592.             HideControl((**doc).vScroll);
  593.             DrawGrowIcon(window);
  594.         }
  595.     }
  596.     
  597. }
  598.  
  599.